//app.js
const api = require('/utils/api.js');
const { baseUrl } = require('/utils/baseUrl.js');
App({
  checkVersionUpdate: function () {
        if (wx.canIUse('getUpdateManager')) {
            const updateManager = wx.getUpdateManager()
            updateManager.onCheckForUpdate(function (res) {
                //请求完新版本信息的回调
                console.log(res.hasUpdate)
            })
            updateManager.onUpdateReady(function () {
                wx.showModal({
                    title: '更新提示',
                    content: '新版本已经准备好，是否重启应用？(注：不更新会导致小程序使用出现未知异常)',
                    success: function (res) {
                        if (res.confirm) {
                            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                            updateManager.applyUpdate()
                        }
                    }
                })
            })
            updateManager.onUpdateFailed(function () {
                wx.showModal({
                    title: '提示',
                    content: '更新小程序失败，请尝试退出小程序后重新进入。(注：不更新会导致小程序使用出现未知异常)',
                    showCancel: false
                })
            })
        } else {
            //TODO 此时微信版本太低（一般而言版本都是支持的）
            wx.showModal({
                title: '溫馨提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。',
                showCancel: false
            })
        }
   },
  onLaunch: function () {
    // 展示本地存储能力
    this.checkVersionUpdate();
    const userInfo = wx.getStorageSync('userInfo')
    const permissionCodes = wx.getStorageSync('permissionCodes')
    this.globalData.userInfo = userInfo ? JSON.parse(userInfo) : {}
    this.globalData.systemInfo = wx.getSystemInfoSync();
    this.globalData.isLogin = wx.getStorageSync("isLogin")
    this.globalData.menuButton = wx.getMenuButtonBoundingClientRect()
    this.globalData.statusBarHeight = (this.globalData.systemInfo.statusBarHeight) * 2
    this.globalData.navBarHeight = (this.globalData.systemInfo.statusBarHeight + this.globalData.menuButton.height + (this.globalData.menuButton.top - this.globalData.systemInfo.statusBarHeight) * 2) * 2
    this.globalData.permissionCodes = permissionCodes ? JSON.parse(permissionCodes) : {}
    this.checkPhoneType(this.globalData.systemInfo)
    // this.globalData.navBarHeight = this.globalData.systemInfo.statusBarHeight
    console.log('1111111111111111111111111111122===');
  },
  checkPhoneType(systemInfo){
    console.log('systemInfo',systemInfo)
    console.log('safeArea',systemInfo.safeArea.top)
    if(systemInfo.safeArea.top > 20 ){
      this.globalData.phoneType = 'abnormal'
    }else{
      this.globalData.phoneType = 'normal'
    }
  },
  onShow () {
    if (wx.getStorageSync("isLogin")) {
      api.post({
        url: 'vx_feedback_unreadCount'
      }).then(res => {
        if (res.data > 0) {
          wx.showTabBarRedDot({
            index: 4
          })
        }
      })
      api.post({
        url: 'operation_add',
        data: {
          operationType: 'activity'
        }
      })
    }
  },
  globalData: {
    permissionCodes:[],
    userInfo: {},
    userDetailInfo: null,
    isLogin: false,
    systemInfo: {},
    CDNPATH: `https://${baseUrl}/h5/static/img/`,
    navBarHeight: 0, // 顶部栏高度 px
  },
  api: api
})